var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var YjxBaseModel = require('./YjxBaseModel');
var CONFIG = require('../../config');


var YjxShowTypeModel = function (obj) {
    YjxBaseModel.call(this,obj);

    // `st_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
    // `parent` bigint(20) DEFAULT NULL COMMENT '父级分类id',
    // `title` varchar(50) NOT NULL DEFAULT '' COMMENT '显示分类名',
    // `desc` varchar(500) DEFAULT '' COMMENT '用途简介',
    // `ver` varchar(10) NOT NULL DEFAULT '0' COMMENT '版本号: 例如6.0',
    // `thumb` text COMMENT '图示: [保留字段]',
    // `order` int(3) NOT NULL DEFAULT '999' COMMENT '显示顺序',
    // `publish` int(1) NOT NULL DEFAULT '0' COMMENT '1发布, 0未发布',
    // `create_time` datetime NOT NULL,
    // `update_time` datetime NOT NULL,
    // `update_by` varchar(50) DEFAULT '',
    // `del_flg` int(1) NOT NULL DEFAULT '0',

};

YjxShowTypeModel.prototype = new YjxBaseModel();

// 取得表名
YjxShowTypeModel.prototype.getTableName = function(){
    return 'yjx_show_type';
};
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
YjxShowTypeModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'st_id='+this.st_id;
};

/**
 * 获取 APP 显示分类信息
 *
 * @author 王俊
 * @date   2017-11-15
 *
 * @param ver 某版本分类, 如: 1.0
 * @param callback 回调
 *            err: 查询出错
 *            types:
 * */
YjxShowTypeModel.prototype.getAppShowTypeInfo = function (ver, callback) {
    pool.pool.getConnection(function (getConErr, connection) {
        if (getConErr) {
            console.log('YjxShowTypeModel.js [getAppShowTypeInfo] getConErr: ' + getConErr);
            return callback(getConErr, null);
        }

        const sql =  "SELECT " +
                    "  str.r_id, " +
                    "  st.st_id, " +
                    "  st.parent, " +
                    "  st.title AS show_type_title, " +
                    "  st.desc AS show_type_desc, " +
                    "  ts.title AS set_title, " +
                    "  ts.desc AS set_desc, " +
                    "  ts.set_id " +
                    "FROM " +
                    "  yjx.yjx_show_type st " +
                    "  LEFT JOIN yjx.yjx_show_type_r str " +
                    "    ON st.st_id = str.st_id " +
                    "  LEFT JOIN yjx.yjx_tmpl_set ts " +
                    "    ON ts.set_id = str.set_id " +
                    "    AND ts.publish = 1 " +
                    "WHERE st.del_flg = 0 " +
                    "  AND st.ver = ver " +
                    "  AND st.publish = 1 " +
                    "  AND str.del_flg = 0 " +
                    "  AND ts.del_flg = 0 " +
                    "  AND ts.use = 0 " +                   // 2018-03-01 modify by wangjun 下辖子分类(模板组)必须是普通模板组(非专题模板组)
                    " ORDER BY ts.order, ts.update_time ";  // 2018-03-01 modify by wangjun 排序方式优先按 order 排序, 后按更新时间排序
        console.log(sql);
        connection.query(sql, function (queryErr, results) {
            connection.release();
            if (queryErr) {
                console.log('YjxShowTypeModel.js [getAppShowTypeInfo] queryErr: ' + queryErr);
                return callback(queryErr, null);
            }
            return callback(queryErr, results);
        });
    })
};

/**
 * 获取 APP 视频子分类信息
 *
 * @author 王俊
 * @date 2018-03-01
 *
 * @param callback
 */
YjxShowTypeModel.prototype.getAppVideoSubTypeInfo = function (callback) {
    pool.pool.getConnection(function (getConErr, connection) {
        if (getConErr) {
            return callback(getConErr, null);
        }

        const sql =
            ' SELECT str.r_id, ' +
            '        st.st_id, ' +
            '        st.parent, ' +
            '        st.title AS show_type_title, ' +
            '        st.desc AS show_type_desc, ' +
            '        ts.title AS set_title, ' +
            '        ts.desc AS set_desc, ' +
            '        ts.set_id, ' +
            '        ts.thumb AS icon ' +
            ' FROM yjx.yjx_show_type st ' +
            ' LEFT JOIN yjx.yjx_show_type_r str ON st.st_id = str.st_id ' +
            ' LEFT JOIN yjx.yjx_tmpl_set ts ON ts.set_id = str.set_id AND ts.publish = 1 ' +
            ' WHERE st.del_flg = 0 ' +
            '   AND st.ver = "1.0" ' +              // 固定分类版本为 1.0
            '   AND st.st_id = 2 ' +                // 固定取 视频 分类
            '   AND st.publish = 1 ' +
            '   AND str.del_flg = 0 ' +
            '   AND ts.del_flg = 0 ' +
            '   AND ts.use = 0 ' +                  // 下辖子分类(模板组)必须是普通模板组(非专题模板组)
            ' ORDER BY ts.order, ts.update_time ';  // 排序方式优先按 order 排序, 后按更新时间排序

        console.log(sql);
        connection.query(sql, function (queryErr, results) {
            connection.release();
            if (queryErr) {
                console.log('YjxShowTypeModel.js [getAppVideoSubTypeInfo] queryErr: ' + queryErr);
                return callback(queryErr, null);
            }
            return callback(queryErr, results);
        });
    })
};


module.exports = YjxShowTypeModel;